package web.servlet.signIn;

import dao.SignInDao;
import domain.LyyBaseResponse;
import domain.LyyBaseServlet;
import domain.SignIn;
import domain.response.AddSignInResponse;
import service.SignInService;
import service.UserService;
import service.impl.SignInServiceImpl;
import service.impl.UserServiceImpl;
import utils.LyyLogUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/addSignIn")
public class AddSignInServlet extends LyyBaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SignInServiceImpl signInService=new SignInServiceImpl();
        AddSignInResponse addSignInResponse=new AddSignInResponse();
        signInService.setCallBack(new SignInDao.QuerySignInDaysCallBack() {
            @Override
            public void onSuccess(int days) {
                LyyLogUtil.logD("lyy-->QuerySignInDaysCallBack onSuccess");
                try {
                    addSignInResponse.setConsecutiveDays(days);
                    addSignInResponse.setSuccessTip("签到成功，经验值加"+getExperience(days)+"！您已连续签到"+days+"天。");
                    UserService service=new UserServiceImpl();
                    service.addUserExperience(request.getParameter("userId"),getExperience(days));
                    jsonResponse(response,addSignInResponse);
                } catch (Exception e) {
                    addSignInResponse.setStatus(0);
                    addSignInResponse.setErrMsg(e.getMessage());
                    try {
                        jsonResponse(response,addSignInResponse);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        Exception exception=null;
        boolean b=false;
        try {
            String userId = request.getParameter("userId");
            SignIn signIn = new SignIn();
            signIn.setUserId(Integer.parseInt(userId));
            signIn.setSignTime(new Date());
            b = signInService.addOne(signIn);
        } catch (Exception e) {
            e.printStackTrace();
            exception=e;
        }


        if(exception!=null){
            addSignInResponse.setStatus(0);
            addSignInResponse.setErrMsg(exception.getMessage());
            jsonResponse(response,addSignInResponse);
        }else {
            if(!b){
                addSignInResponse.setStatus(0);
                addSignInResponse.setErrMsg("签到失败！");
                jsonResponse(response,addSignInResponse);
            }
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    private static int getExperience(int day){
        if(day>7){
            day=7;//连续签到7天，获得的经验值到达峰值
        }
        int result=5;
        int d=2;//公差
        result=result+(day-1)*d;
        return result;
    }
}
